//
//  StatusCell.swift
//  SinaWeiBo
//
//  Created by gxiangzi on 15/9/6.
//  Copyright © 2015年 hqu. All rights reserved.
//

import UIKit

let GXStatusCellMargin:CGFloat = 12
let GXStatusIconWidth: CGFloat = 35

/// 默认的图片大小
let GXStatusPictureItemWidth: CGFloat = 90
/// 默认的图片间距
let GXStatusPictureItemMargin: CGFloat = 10
/// 每行的最大图片数量
let GXStatusPictureMaxCount: CGFloat = 3
/// 配图视图的最大的尺寸
let GXStatusPictureMaxWidth = GXStatusPictureMaxCount * GXStatusPictureItemWidth + (GXStatusPictureMaxCount - 1) * GXStatusPictureItemMargin

class StatusCell: UITableViewCell {

    
    var statusViewModel: StatusViewModel? {
        didSet {
            // setter 方法里面需要的数据
            topView.statusViewModel = statusViewModel
            contentLabel.text =  statusViewModel?.status.text
            
            pictureView.statusViewModel = statusViewModel
            
            // 更新frame
            pictureViewWidthCons?.constant = pictureView.bounds.width
            pictureViewHeightCons?.constant = pictureView.bounds.height
            
            // 更新新的约束
            // 不会进入到 pictureView.calcViewSize() 中
//            pictureViewHeightCons?.constant = pictureView.calcViewSize().width
//            pictureViewHeightCons?.constant = pictureView.calcViewSize().height

//            printLog(pictureView)
            
            // 根据图片，决定是否要顶部约束
            pictureViewTopCosn?.constant = statusViewModel?.thumbnail?.count == 0 ? 0 : GXStatusCellMargin
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 计算指定视图模型对应的行高
    ///
    /// - parameter viewModel: viewModel
    ///
    /// - returns: 计算的行高
    func rowHeight(viewModel: StatusViewModel) -> CGFloat {
        
        // 1. 设置视图模型 － 会调用 模型的 didSet
        statusViewModel = viewModel
        
        // 2. 有了内容之后更新约束
        layoutIfNeeded()
        
        return CGRectGetMaxY(bottonView.frame)
    }

    
    
    /// 定义两个属性，来记录picture的约束
    var pictureViewWidthCons : NSLayoutConstraint?
    var pictureViewHeightCons : NSLayoutConstraint?
    var pictureViewTopCosn: NSLayoutConstraint?
    
     func setupUI() {
        
        backgroundColor = UIColor.whiteColor()
        
        let topSep = UIView()
        topSep.backgroundColor = UIColor(white: 0.85, alpha: 1.0)
        
        // 1.添加控件
        contentView.addSubview(topSep)
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottonView)
        
        // 2. 设置布局，cell 的默认大小是 320/44，iPhone 4时代的遗留问题，宽度不能直接使用cell的宽度
        let width = UIScreen.mainScreen().bounds.width
        
        // 1> 顶部分割线
        topSep.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: width, height: 10))
        
        // 2> 顶部的视图
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: topSep, size: CGSize(width: width, height: GXStatusIconWidth +   GXStatusCellMargin), offset: CGPoint(x: 0, y: 10))
        
        // 3> 文本
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: GXStatusCellMargin, y: GXStatusCellMargin))
        
        // 4> 配图
        // 不能这样写，因为还没有对cell 赋值的时候，就调用了该方法
//        let  size = pictureView.calcViewSize()
        
        
        // 5> 底部视图
        bottonView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: width, height: 44), offset: CGPoint(x: -GXStatusCellMargin, y: GXStatusCellMargin))
        
        // 指定底部视图相对底边约束,配合 估算行高一起使用即可
//        bottonView.ff_AlignInner(type: ff_AlignType.BottomRight, referView: contentView, size: nil)
        
    }
    
    
    private lazy var topView: StatusCellTopView = StatusCellTopView()

    lazy var contentLabel = UILabel(title: nil, color: UIColor.darkGrayColor(), fontSize: 15, layoutWidth: UIScreen.mainScreen().bounds.width - 2 * GXStatusCellMargin)
    
    lazy var pictureView : StatusPictureView  = StatusPictureView()
    
    lazy var bottonView : StatusCellBottomView = StatusCellBottomView()

}
